#!/usr/bin/python
"""
This program detects objects in drawings

Written by: Elias Johansson
"""
import sys, Image, opencv
from opencv.cv import *
from opencv.highgui import *

storage = cvCreateMemStorage(0)


def gray(img):
	gray = cvCreateImage(cvSize(img.width, img.height), 8, 1)
	cvCvtColor( img, gray, CV_BGR2GRAY )
	return gray

def threshold(img, method):
	edge = cvCreateImage(cvSize(img.width, img.height), 8, 1)	
	cvThreshold(img, edge, 220, 255, method)
	return edge

def contours(edge, img, size, outer, hole):
	cvClearMemStorage( storage )
	nc, cs = cvFindContours(edge, storage)
	for c in cs.hrange(): 
		cvDrawContours(img, c, outer, hole, 0,size)
	return img

def detect(name):
	img = cvLoadImage(name, 1)
	
	gray = cvCreateImage(cvSize(img.width, img.height), 8, 1)
	cvCvtColor( img, gray, CV_BGR2GRAY )
	
	edge1 = cvCreateImage(cvSize(img.width, img.height), 8, 1)
	edge2 = cvCreateImage(cvSize(img.width, img.height), 8, 1)	

	cvThreshold(gray,edge1,220,255,CV_THRESH_BINARY_INV)
	
	cvClearMemStorage( storage )

	nc, cs = cvFindContours(edge1, storage)

	for c in cs.hrange(): 
		cvDrawContours(edge2, c, CV_RGB(255,255,255) ,CV_RGB(255,255,255), 0,-1)

	cvThreshold(edge2,edge1,220,255,CV_THRESH_BINARY)

	cvClearMemStorage( storage )

	nc, cs = cvFindContours(edge1, storage)

	for c in cs.hrange(): 
		cvDrawContours(edge2, c, CV_RGB(255,0,0) ,CV_RGB(0,0,255), 0,1)

	cvThreshold(edge2,edge1,220,255,CV_THRESH_BINARY)
	
	cvClearMemStorage( storage )
	nc, cs = cvFindContours(edge1, storage)

	for c in cs.hrange(): 
		rect = cvBoundingRect(c,0)
		if (rect.width)>20 and (rect.height)>20: 
			cvRectangle(img, (rect.x,rect.y),(rect.x+rect.width,rect.y+rect.height),CV_RGB(255,0,0), 2)
	return img 

if __name__ == '__main__':

	argc = len(sys.argv)
	if(argc == 3):
		if(sys.argv[1] == "-g"):
			img = cvLoadImage(sys.argv[2], 1)
			cvNamedWindow("Gray")
			cvShowImage("Gray",gray(img))
			cvWaitKey()
		elif(sys.argv[1] == "-t"):
			img = cvLoadImage(sys.argv[2], 1)
			cvNamedWindow("Threshold")
			cvShowImage("Threshold",threshold(gray(img), CV_THRESH_BINARY_INV))
			cvWaitKey()
		elif(sys.argv[1] == "-c1"):
			img = cvLoadImage(sys.argv[2], 1)
			t = threshold(gray(img), CV_THRESH_BINARY_INV)
			cvNamedWindow("Contours")
			cvShowImage("Contours",contours(t,img,2,CV_RGB(255,0,0),CV_RGB(0,0,255)))
			cvWaitKey()
		elif(sys.argv[1] == "-c2"):
			img = cvLoadImage(sys.argv[2], 1)
			t1 = threshold(gray(img), CV_THRESH_BINARY_INV)
			black = cvCreateImage(cvSize(img.width, img.height), 8, 1)
			c = contours(t1,black,-1,CV_RGB(255,255,255),CV_RGB(255,255,255))
			del t1
			t2 = threshold(c, CV_THRESH_BINARY)
			cvNamedWindow("Contours")
			cvShowImage("Contours",contours(t2,img,2,CV_RGB(255,0,0),CV_RGB(0,0,255)))
			cvWaitKey()
		elif(sys.argv[1] == "-d"):
			cvNamedWindow("Objects")
			cvShowImage("Objects",detect(sys.argv[2]))
			cvWaitKey()
		else:
			print("To run: python detect.py [-g -t -c1 -c2 -c3] file\n\t-g show the gray image\n\t-t show the threshold image\n\t-c1 show the contours on the image, filtered one time\n\t-c2 show the contours on the image, filtered twice\n\t-d show the objects in the image")

